<template>
  <view>
    <view class="order-index" ref="container">
      <view class="headerback"></view>
      <view class="header">
        <div class="header_count">
          <swiper
            indicator-dots="true"
            :circular="circular"
            indicator-color="#E4E4E4"
            indicator-active-color="#E93323"
            previous-margin="0"
            next-margin="0"
            :current="swiperCur"
            @change="swiperChange"
          >
            <block>
              <swiper-item
                :class="{ active: 0 == swiperCur }"
                style="background: #ff6b00"
              >
                <view class="slide-navigator">
                  <!-- <view class="item" hover-class="none" @click="pupoud()">
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_add.png"
                    ></image>
                    <text class="text">添加品牌</text>
                  </view> -->
                  <view class="item" hover-class="none" @click="jumpAddGoods">
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_add.png"
                    ></image>
                    <text class="text">添加商品</text>
                  </view>
                  <navigator
                    class="item"
                    :url="`/pages/product/goodsOnSale/index?mer_id=${mer_id}&type=1`"
                    hover-class="none"
                  >
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_sales.png"
                    ></image>
                    <text class="text">在售商品</text>
                  </navigator>
                  <navigator
                    class="item"
                    :url="`/pages/product/goodsOnSale/index?mer_id=${mer_id}&type=3`"
                    hover-class="none"
                  >
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_out.png"
                    ></image>
                    <text class="text">售罄商品</text>
                  </navigator>
                  <navigator
                    class="item"
                    :url="`/pages/product/goodsOnSale/index?mer_id=${mer_id}&type=5`"
                    hover-class="none"
                  >
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_recycle.png"
                    ></image>
                    <text class="text">已下架</text>
                  </navigator>
                  <navigator
                    class="item"
                    :url="`/pages/product/storeClassification/index?mer_id=${mer_id}`"
                    hover-class="none"
                  >
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_cate.png"
                    ></image>
                    <text class="text">店铺分类</text>
                  </navigator>
                </view>
              </swiper-item>
              <swiper-item :class="{ active: 1 == swiperCur }">
                <view class="slide-navigator">
                  <navigator
                    class="item"
                    :url="`/pages/product/addGoods/freightTemplate?mer_id=${mer_id}`"
                    hover-class="none"
                  >
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_freight.png"
                    ></image>
                    <text class="text">运费模板</text>
                  </navigator>
                  <navigator
                    class="item"
                    :url="`/pages/product/addGoods/mulSpecification?mer_id=${mer_id}`"
                    hover-class="none"
                  >
                    <image
                      mode="widthFix"
                      class="image"
                      src="../static/images/product_specification.png"
                    ></image>
                    <text class="text">规格模板</text>
                  </navigator>
                </view>
              </swiper-item>
            </block>
          </swiper>
        </div>
      </view>
      <view class="wrapper">
        <view class="product_list acea-row row-between-wrapper">
          <block v-for="(item, index) in productList" :key="index">
            <view class="item">
              <view class="image">
                <image :src="item.image"></image>
                <text v-if="item.spec_type == 1" class="spec">多规格</text>
              </view>
              <view class="text">
                <view class="name">
                  <text v-if="item.is_gift_bag" class="font-org">礼包</text>
                  <text class="text_name acea-row line1">{{
                    item.store_name
                  }}</text>
                </view>
                <view class="sales">
                  <text class="num">库存: {{ item.stock }}</text>
                  <text class="num">销量: {{ item.sales }}</text>
                </view>
                <view class="money-wrap">
                  <text class="price" style="color: #ff6b00"
                    >￥{{ item.price }} / 天</text
                  >
                  <text class="ot_price"
                    >起租天数 : {{ item.lease_day }} 天</text
                  >
                </view>
                <view class="operation">
                  <view></view>
                  <view class="acea-row row-middle">
                    <view
                      v-if="item.is_show == 1 && item.status == 1"
                      class="bnt"
                      @tap.stop="handleShelves(item, 0)"
                      >下架</view
                    >
                    <view
                      v-if="item.is_show == 0 && item.status == 1"
                      class="bnt"
                      @tap.stop="handleShelves(item), 1"
                      >上架</view
                    >
                    <view @click="editGoods(item)" class="bnt">编辑</view>
                    <!-- <navigator :url="'/pages/admin/goods_details/index?product_id='+item.product_id+'&product_type=0'" class="bnt" hover-class='none'>办单</navigator> -->
                    <!-- <navigator
                  :url="'/pages/admin/imageIndex/index'"
                  hover-class="none"
                ></navigator> -->
                    <view class="bnt" @click="navigateToPage(item)">办单</view>
                    <view
                      class="bnt"
                      v-if="item.is_show == 0"
                      @tap.stop="handleRecycle(item, index)"
                      >删除</view
                    >
                    <!-- <view class="bnt bnt_recommend" v-if="item.is_show == 1" @tap.stop="handleRecommend(item)">{{item.is_good ? '取消推荐' : '店铺推荐'}}</view> -->
                  </view>
                </view>
              </view>
            </view>
          </block>
        </view>
      </view>
      <Loading :loaded="loaded" :loading="loading"></Loading>
      <!-- 绑定手机号 -->

      <!-- <uni-popup :show="showPopup" @close="showPopup = false">
      </uni-popup> -->
    </view>
    <u-popup v-model="reasonDialog" mode="center" border-radius="14">
      <view class="cha" @click="outis()">×</view>
      <view
        style="padding: 20rpx; width: 200px; height: 200px; margin-top: 20rpx"
      >
        <image
          style="width: 100%; height: 100%"
          :src="imagesImg"
          mode=""
        ></image>
      </view>
    </u-popup>
    <u-popup v-model="pupoudIs" mode="center" border-radius="14">
      <!-- <view class="cha" @click="outis()">×</view> -->
      <view style="padding: 20rpx; margin-top: 20rpx">
        <view style="display: flex">
          <view style="width: 500rpx">
            <input v-model="brand" type="text" placeholder="请输入品牌" />
          </view>
          <view
            style="background: #0683f8; color: #fff; padding: 10rpx 20rpx"
            @click="Deliverycreate()"
            >确认</view
          >
        </view>

        <view v-for="(item, index) in dataList" :key="index">
          <view
            style="
              display: flex;
              justify-content: space-between;
              padding: 10rpx 0;
            "
          >
            <view>{{ item.brand_name }}</view>
            <view style="font-size: 30rpx" @click="deleteIs(item)">×</view>
          </view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  productLstApi,
  productDeleteApi,
  productOffApi,
  productRecommendApi,
  productQrcode,
  getDeliverycreate,
  categorylist,
  categorydelete,
} from "@/api/product";
import Loading from "@/components/Loading/index.vue";
import {
  navigateTo,
  navigateBack,
  serialize,
  setStorage,
  getStorage,
  removeStorage,
  Toast,
  Modal,
} from "../../../libs/uniApi.js";
export default {
  name: "productList",
  components: {
    Loading,
  },
  data() {
    return {
      current: "",
      where: {
        page: 1,
        limit: 20,
      },
      loaded: false,
      loading: false,
      mer_id: "",
      productList: [],
      swiperCur: 0,
      circular: true,
      id: 0,
      reasonDialog: false,
      imagesImg: "",
      pupoudIs: false,
      brand: "",
      dataList: [],
    };
  },
  onLoad(options) {
    this.mer_id = options.mer_id;
    this.getList(this.mer_id);
  },
  methods: {
    deleteIs(item) {
      let dataData = {};
      dataData["brand_id"] = item.brand_id;

      categorydelete(this.mer_id, dataData).then((res) => {
        console.log(res);
        this.pupoudIs = false;
        this.brand = "";
      });
    },
    Deliverycreate() {
      let dataData = {};
      let pormise = this.brand;
      dataData["brand_name"] = pormise;
      console.log(this.mer_id);
      console.log(dataData);
      getDeliverycreate(this.mer_id, dataData).then((res) => {
        console.log(res);
        that.pupoudIs = false;
        this.brand = "";
      });
    },
    pupoud() {
      let that = this;
      that.pupoudIs = true;

      categorylist(this.mer_id).then((res) => {
        console.log(res);
        console.log(res.data);
        this.dataList = res.data;
        console.log(this.dataList);
      });
    },
    navigateToPage(e) {
      console.log(e.product_id);
      // uni.navigateTo({
      //   url: "/pages/admin/imageIndex/index", // 假设要跳转到的页面路径为/pages/target/target
      // });
      this.id = e.product_id;

      let that = this;
      console.log(that.id);
      let id = that.id;
      let merId = that.mer_id;
      productQrcode(merId, id).then(async (res) => {
        const base64Image = res.data.url; // 这里应该是你的base64字符串
        that.imagesImg = base64Image.replace(/[\r\n]/g, "");
        that.reasonDialog = true;
      });
    },
    outis() {
      this.reasonDialog = false;
    },
    // 跳转添加商品界面
    jumpAddGoods() {
      const data = getStorage("addGoodsFormData");
      if (data && data.product_id) {
        let waitDeleteData = [
          "addGoodsFormData",
          "singleSpecification",
          "attrValue",
          "modifyPriceData",
          "addGoodsSecoundData",
          "goodsDis",
          "editGoodsDetils",
        ];
        waitDeleteData.forEach((item) => {
          if (getStorage(item)) {
            removeStorage(item);
          }
        });
      }
      navigateTo(1, "/pages/product/addGoods/index", { mer_id: this.mer_id });
    },
    // swiper
    swiperChange(e) {
      let { current, source } = e.detail;
      if (source === "touch") {
        //根据官方 source 来进行判断swiper的change事件是通过什么来触发的，autoplay是自动轮播。touch是用户手动滑动。其他的就是未知问题。抖动问题主要由于未知问题引起的，所以做了限制，只有在自动轮播和用户主动触发才去改变current值，达到规避了抖动bug
        this.swiperCur = e.detail.current;
      }
    },
    getList: function (mer_id) {
      var that = this;
      if (that.loading || that.loaded) return;
      that.loading = true;
      productLstApi(mer_id, that.where).then(
        (res) => {
          that.loading = false;
          that.loaded = res.data.list.length < that.where.limit;
          that.productList.push.apply(that.productList, res.data.list);
          that.where.page = that.where.page + 1;
        },
        (error) => {
          that.$util.Tips({
            title: error.msg,
          });
        }
      );
    },
    editGoods(item) {
      let waitDeleteData = [
        "addGoodsFormData",
        "singleSpecification",
        "attrValue",
        "modifyPriceData",
        "addGoodsSecoundData",
        "goodsDis",
        "editGoodsDetils",
        "canChange",
        "canChangeSecound",
      ];
      waitDeleteData.forEach((item) => {
        if (getStorage(item)) {
          removeStorage(item);
        }
      });
      navigateTo(1, "/pages/product/addGoods/index", {
        mer_id: item.mer_id,
        product_id: item.product_id,
      });
    },
    handleRecycle(item, index) {
      let that = this;
      Modal(
        "温馨提示",
        `商品"${item.store_name}"将被已下架，请问是否继续?`
      ).then((res) => {
        productDeleteApi(that.mer_id, item.product_id)
          .then((res) => {
            that.$util.Tips(
              {
                title: res.message,
                icon: "success",
              },
              () => {
                that.productList.splice(index, 1);
              }
            );
          })
          .catch((rej) => {
            Toast(`${rej}, 已下架失败`);
          });
      });
    },
    //下架 0
    handleShelves(item, status) {
      let that = this;
      if (status == 0) {
        uni.showModal({
          content: "确定要下架该商品么？",
          success: function (res) {
            if (res.confirm) {
              that.onAndOff(item, 0);
              uni.showToast({
                title: "下架成功",
                icon: "none",
              });
            } else if (res.cancel) {
              console.log("用户点击取消");
            }
          },
        });
      } else {
        that.onAndOff(item, 1);
      }
    },
    //上下架
    onAndOff(item, status) {
      let that = this;
      productOffApi(that.mer_id, item.product_id, { status: status })
        .then((res) => {
          that.$util.Tips(
            {
              title: res.message,
              icon: "success",
            },
            () => {
              that.$set(item, "is_show", item.is_show == 0 ? 1 : 0);
            }
          );
        })
        .catch((err) => {
          return that.$util.Tips({
            title: err,
          });
        });
    },
    //设置推荐
    handleRecommend(item) {
      let that = this;
      let is_good = item.is_good ? 0 : 1;
      productRecommendApi(that.mer_id, item.product_id, { is_good: is_good })
        .then((res) => {
          that.$util.Tips(
            {
              title: res.message,
              icon: "success",
            },
            () => {
              that.$set(item, "is_good", item.is_good == 0 ? 1 : 0);
            }
          );
        })
        .catch((err) => {
          return that.$util.Tips({
            title: err,
          });
        });
    },
    more(index) {
      this.current = index;
    },
  },
  onReachBottom() {
    this.getList(this.mer_id);
  },
};
</script>

<style scoped lang="scss">
page {
  background-color: #f5f5f5;
}
.order-index {
  position: relative;
  // background-image: url("");
  // background: #ff6b00;
  // background-repeat: no-repeat;
  // background-size: 100%;
  .headerback {
    top: 0;
    width: 100%;
    height: 500rpx;
    position: absolute;
    background: #ff6b00;
  }
}
.popupn {
  position: fixed;
  width: 100%;
  text-align: center;
  top: 0;
  left: 0;
  background: transparent;
  height: 90rpx;
  line-height: 90rpx;
  z-index: 5;
  .spin {
    display: block;
    transform: rotate(180deg);
    font-size: 36rpx;
  }
  .title {
    max-width: 560rpx;
    margin: 0 auto;
    position: relative;
    display: inline;
    padding: 10rpx 30rpx 10rpx 50rpx;
    background-color: rgba(0, 0, 0, 0.25);
    border-radius: 30rpx;
    color: #ffffff;
  }
  .iconfont {
    display: inline-block;
    position: relative;
    top: 4rpx;
    right: 0;
  }
  .mer_logo {
    width: 34rpx;
    height: 34rpx;
    position: relative;
    top: 6rpx;
    right: 10px;
  }
  .mer_name {
    display: inline-block;
    max-width: 650rpx;
  }
  .invoice-content {
    background-color: #ffffff;
  }
}

/deep/.uni-swiper-slides {
  inset: 0 !important;
}
uni-swiper,
swiper {
  height: 200rpx;
}
.header {
  padding-top: 40rpx;
  margin: 0 20rpx;
  .header_count {
    width: 710rpx;
    padding: 30rpx 30rpx 0;
    background: #ff6b00;
    border-radius: 10rpx;
    position: relative;
  }
  .item {
    display: inline-block;
    width: 130rpx;
    text-align: center;
    .image {
      width: 84rpx;
      height: 84rpx;
    }
    .text {
      display: block;
      text-align: center;
      color: #fff;
      font-size: 24rpx;
      margin-top: 18rpx;
    }
  }
}
/deep/.uni-swiper-slides {
  width: 650rpx;
}
/deep/.uni-swiper-dot {
  width: 15rpx;
  height: 5rpx;
}
/deep/.uni-swiper-dots-horizontal .uni-swiper-dot {
  margin-right: 0;
}
.product_list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 100%;
  padding: 0 20rpx;
  .item {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    padding: 30rpx 20rpx;
    border-radius: 0;
    margin-bottom: 0;
    background: #ffffff;
    margin-top: 30rpx;
    border-radius: 10rpx 10rpx 0 0;
    .image,
    image {
      width: 150rpx;
      height: 150rpx;
      border-radius: 16rpx;
      position: relative;
      .spec {
        color: #ffffff;
        background: rgba(0, 0, 0, 0.5);
        border-radius: 16rpx 0 16rpx 0;
        text-align: center;
        font-size: 18rpx;
        display: inline-block;
        width: 84rpx;
        line-height: 30rpx;
        position: absolute;
        bottom: 0;
        right: 0;
      }
      uni-image {
        width: 150rpx;
        height: 150rpx;
        border-radius: 16rpx;
      }
    }
    .text {
      position: relative;
      flex: 1;
      min-width: 0;
      padding: 10rpx 10rpx 20rpx 30rpx;
      .name {
        color: #282828;
        font-size: 30rpx;
        font-family: "PingFang SC";
        display: flex;
        align-items: center;
      }
      .text_name {
        display: inline-block;
        max-width: 400rpx;
      }
      .font-org {
        background-color: #fc8327;

        display: inline-block;
        color: #fff;
        font-size: 16rpx;
        text-align: center;
        border-radius: 5rpx;
        padding: 0 4rpx;
        line-height: 28rpx;
        margin-right: 8rpx;
      }
      .money-wrap {
        margin-top: 5rpx;
        .price {
          font-size: 30rpx;
          display: inline-block;
          margin-right: 15rpx;
        }
        .ot_price {
          color: #bebebe;
          font-size: 26rpx;
        }
      }
      .sales {
        margin-top: 5rpx;
        color: #868686;
        font-size: 22rpx;
        .num {
          display: inline-block;
          margin-right: 20rpx;
        }
      }
    }
  }
  .operation {
    padding: 10upx 0upx;
    background: #ffffff;
    width: 100%;
    border-radius: 0 0 10rpx 10rpx;
    .bnt {
      font-size: 26rpx;
      color: #999999;
      width: 120rpx;
      height: 58rpx;
      border-radius: 30rpx;
      border: 1px solid #999999;
      text-align: center;
      line-height: 58rpx;
      ~ .bnt {
        margin-left: 18rpx;
      }
      &.bnt_recommend {
        width: 160rpx;
        color: #ffffff;
        background: linear-gradient(135deg, #fd6523 0%, #e93323 100%);
        border-color: transparent;
      }
    }
  }
}
.cha {
  font-size: 50rpx;
  position: absolute;
  right: 4rpx;
  top: -16rpx;
}
</style>
